$regfile = "m16def.dat"
$crystal = 8000000

'Inisialisasi variable Global
Dim Input_pinb As Integer , Icadc As Byte , Iccheck As Byte , Itemp As Long
Dim Winput_adc(6) As Word , Winput_adc0(6) As Word , Idisplay As Byte
Dim Wref_adc(6) As Word , Wref_adc0(6) As Word , Wtemp As Word
Dim Slcd1 As String * 16 , Slcd2 As String * 16 , Stemp As String * 16
Dim Sitemp As Single

'Inisialisasi Variable Constant
Const Cowref_adc1 = 0
Const Cowref_adc2 = 2
Const Cowref_adc3 = 4
Const Cowref_adc4 = 6
Const Cowref_adc5 = 8
Const Cowref_adc6 = 10
Const Kfwd = 0.005
Const Krfel = 0.005
Const Krfin = 0.005
Const Kvpa = 0.005
Const Kipa = 0.005
Const Ktemp = 0.005

'Deklarasi fungsi
Declare Sub Finisialisasi()
Declare Sub Feepwrite()

'Deklarasi vektor interupsi
On Timer0 Fint_handle_timer0

'Fungsi Utama
Fmain:
    Dim Case0 As Byte
    Call Finisialisasi()
    Locate 1 , 1
    Lcd "  W E L C O M E "
    Locate 2 , 1
    Lcd " SBE 500 RF AMP "
    Waitms 1000
    Do
       Select Case Idisplay
          Case 0:
          Case 1:
              If Case0 <> 1 Then
                  Cls
                  Locate 1 , 1
                  Lcd "Forward:    WATT"
                  Locate 2 , 1
                  Lcd "MAX    :    WATT"
                  Locate 1 , 9
                  Sitemp = Winput_adc(1) * Kfwd
                  Stemp = Fusing(sitemp , "#.##")
                  Lcd Stemp
                  Locate 2 , 9
                  Sitemp = Wref_adc(1) * Kfwd
                  Stemp = Fusing(sitemp , "#.##")
                  Lcd Stemp
                  Case0 = 1
              End If
              If Winput_adc0(1) <> Winput_adc(1) Then
                  Locate 1 , 9
                  Sitemp = Winput_adc(1) * Kfwd
                  Stemp = Fusing(sitemp , "#.##")
                  Lcd Stemp
                  Winput_adc0(1) = Winput_adc(1)
              End If
              If Wref_adc0(1) <> Wref_adc(1) Then
                  Locate 2 , 9
                  Sitemp = Wref_adc(1) * Kfwd
                  Stemp = Fusing(sitemp , "#.##")
                  Lcd Stemp
                  Wref_adc0(1) = Wref_adc(1)
              End If
          Case 2:
              If Case0 <> 2 Then
                  Cls
                  Locate 1 , 1
                  Lcd "REFL   :    WATT"
                  Locate 2 , 1
                  Lcd "MAX    :    WATT"
                  Locate 1 , 9
                  Sitemp = Winput_adc(2) * Krfel
                  Stemp = Fusing(sitemp , "#.##")
                  Lcd Stemp
                  Locate 2 , 9
                  Sitemp = Wref_adc(2) * Krfel
                  Stemp = Fusing(sitemp , "#.##")
                  Lcd Stemp
                  Case0 = 2
              End If
              If Winput_adc0(2) <> Winput_adc(2) Then
                  Locate 1 , 9
                  Sitemp = Winput_adc(2) * Krfel
                  Stemp = Fusing(sitemp , "#.##")
                  Lcd Stemp
                  Winput_adc0(2) = Winput_adc(2)
              End If
              If Wref_adc0(2) <> Wref_adc(2) Then
                  Locate 2 , 9
                  Sitemp = Wref_adc(2) * Krfel
                  Stemp = Fusing(sitemp , "#.##")
                  Lcd Stemp
                  Wref_adc0(2) = Wref_adc(2)
              End If
          Case 3:
              If Case0 <> 3 Then
                  Cls
                  Locate 1 , 1
                  Lcd "RF In  :    WATT"
                  Locate 2 , 1
                  Lcd "MAX    :    WATT"
                  Locate 1 , 9
                  Sitemp = Winput_adc(3) * Krfin
                  Stemp = Fusing(sitemp , "#.##")
                  Lcd Stemp
                  Locate 2 , 9
                  Sitemp = Wref_adc(3) * Krfin
                  Stemp = Fusing(sitemp , "#.##")
                  Lcd Stemp
                  Case0 = 3
              End If
              If Winput_adc0(3) <> Winput_adc(3) Then
                  Locate 1 , 9
                  Sitemp = Winput_adc(3) * Krfin
                  Stemp = Fusing(sitemp , "#.##")
                  Lcd Stemp
                  Winput_adc0(3) = Winput_adc(3)
              End If
              If Wref_adc0(3) <> Wref_adc(3) Then
                  Locate 2 , 9
                  Sitemp = Wref_adc(3) * Krfin
                  Stemp = Fusing(sitemp , "#.##")
                  Lcd Stemp
                  Wref_adc0(3) = Wref_adc(3)
              End If
          Case 4:
              If Case0 <> 4 Then
                  Cls
                  Locate 1 , 1
                  Lcd "VPA    :    VOLT"
                  Locate 2 , 1
                  Lcd "MAX    :    VOLT"
                  Locate 1 , 9
                  Sitemp = Winput_adc(4) * Kvpa
                  Stemp = Fusing(sitemp , "#.##")
                  Lcd Stemp
                  Locate 2 , 9
                  Sitemp = Wref_adc(4) * Kvpa
                  Stemp = Fusing(sitemp , "#.##")
                  Lcd Stemp
                  Case0 = 4
              End If
              If Winput_adc0(4) <> Winput_adc(4) Then
                  Locate 1 , 9
                  Sitemp = Winput_adc(4) * Kvpa
                  Stemp = Fusing(sitemp , "#.##")
                  Lcd Stemp
                  Winput_adc0(4) = Winput_adc(4)
              End If
              If Wref_adc0(4) <> Wref_adc(4) Then
                  Locate 2 , 9
                  Sitemp = Wref_adc(4) * Kvpa
                  Stemp = Fusing(sitemp , "#.##")
                  Lcd Stemp
                  Wref_adc0(4) = Wref_adc(4)
              End If
          Case 5:
              If Case0 <> 5 Then
                  Cls
                  Locate 1 , 1
                  Lcd "IPA    :    AMP "
                  Locate 2 , 1
                  Lcd "MAX    :    AMP "
                  Locate 1 , 9
                  Sitemp = Winput_adc(5) * Kipa
                  Stemp = Fusing(sitemp , "#.##")
                  Lcd Stemp
                  Locate 2 , 9
                  Sitemp = Wref_adc(5) * Kipa
                  Stemp = Fusing(sitemp , "#.##")
                  Lcd Stemp
                  Case0 = 5
              End If
              If Winput_adc0(5) <> Winput_adc(5) Then
                  Locate 1 , 9
                  Sitemp = Winput_adc(5) * Kipa
                  Stemp = Fusing(sitemp , "#.##")
                  Lcd Stemp
                  Winput_adc0(5) = Winput_adc(5)
              End If
              If Wref_adc0(5) <> Wref_adc(5) Then
                  Locate 2 , 9
                  Sitemp = Wref_adc(5) * Kipa
                  Stemp = Fusing(sitemp , "#.##")
                  Lcd Stemp
                  Wref_adc0(5) = Wref_adc(5)
              End If
          Case 6:
              If Case0 <> 3 Then
                  Cls
                  Locate 1 , 1
                  Lcd "Temp   :       C"
                  Locate 2 , 1
                  Lcd "MAX    :       C"
                  Locate 1 , 9
                  Sitemp = Winput_adc(6) * Ktemp
                  Stemp = Fusing(sitemp , "#.##")
                  Lcd Stemp
                  Locate 2 , 9
                  Sitemp = Wref_adc(6) * Ktemp
                  Stemp = Fusing(sitemp , "#.##")
                  Lcd Stemp
                  Case0 = 3
              End If
              If Winput_adc0(6) <> Winput_adc(6) Then
                  Locate 1 , 9
                  Sitemp = Winput_adc(6) * Ktemp
                  Stemp = Fusing(sitemp , "#.##")
                  Lcd Stemp
                  Winput_adc0(6) = Winput_adc(6)
              End If
              If Wref_adc0(6) <> Wref_adc(6) Then
                  Locate 2 , 9
                  Sitemp = Wref_adc(6) * Ktemp
                  Stemp = Fusing(sitemp , "#.##")
                  Lcd Stemp
                  Wref_adc0(6) = Wref_adc(6)
              End If
          Case 7:
              If Case0 <> 7 Then
                  Cls
                  Locate 1 , 1
                  Lcd "PROTECT:       X"
                  Locate 2 , 1
                  Lcd "MAX    :       X"
                  Locate 1 , 9
                  Sitemp = 1
                  Stemp = Fusing(sitemp , "#.##")
                  Lcd Stemp
                  Locate 2 , 9
                  Stemp = Fusing(Sitemp , "#.##")
                  Lcd Stemp
                  Case0 = 7
              End If
          Case 8:
              If Case0 <> 8 Then
                  Cls
                  Locate 1 , 1
                  Lcd "   DESIGN BY:   "
                  Locate 2 , 1
                  Lcd " SBE- EQUIPMENT "
                  Case0 = 8
              End If
          Case Else:
       End Select
    Loop
End

'Fungsi Interupsi Timer0
Fint_handle_timer0:
   Dim Ichannel As Integer

   Icadc = Icadc + 1
   Iccheck = Iccheck + 1
   If Icadc > 4 Then                                        '1 ms
       'Baca data input ADC
       For Ichannel = 1 To 6
           Wtemp = Getadc(ichannel - 1)
           Winput_adc(ichannel) = Wtemp
           If Winput_adc(ichannel) > Wref_adc(ichannel ) Then
               Itemp = 1
               Shift Itemp , Left , Ichannel
               Portc = Portc Or Itemp
           Else
               Itemp = 1
               Shift Itemp , Left , Ichannel
               Itemp = Not Itemp
               Portc = Portc And Itemp
           End If
       Next Ichannel
       Icadc = 0
   End If
   If Iccheck > 40 Then                                     '10 ms
       Input_pinb = Pinb And 1
       'Input_pinb = 0
       If Pinb.0 = 0 Then
           Portc = &H07
       End If
       Iccheck = 0
       Wtemp = Getadc(0)
       Cls
       Lcd Wtemp
   End If
Return

Sub Finisialisasi()
     'Inisialisasi Port
     Config Portc = Output
     Config Portd = Output
     Config Portb = Input
     '-Aktivasi internal pull up resistor pada PORTB
     Portb = &HFF

     'Inisialisasi LCD
     Config Lcdpin = Pin , Db4 = Portd.4 , Db5 = Portd.5 , Db6 = Portd.6 , Db7 = Portd.7 , E = Portd.2 , Rs = Portd.0
     Config Lcd = 16 * 2
     Initlcd
     Cursor Off Noblink
     Cls

     'Inisialisasi Timer0
     Config Timer0 = Timer , Prescale = 8

     'Inisialisasi ADC
     Config Adc = Single , Prescaler = Auto , Reference = Off

     Start Adc

     'Inisialisasi Interupsi
     Enable Interrupts
     '-inisialisasi Interupsi Timer0
     Enable Timer0
     Start Timer0

     'Inisialisasi EEPROM
     $eeprom

     'Inisialisasi variable awal
     Icadc = 0
     Iccheck = 0
     Readeeprom Wref_adc(1) , Cowref_adc1
     Readeeprom Wref_adc(2) , Cowref_adc2
     Readeeprom Wref_adc(3) , Cowref_adc3
     Readeeprom Wref_adc(4) , Cowref_adc4
     Readeeprom Wref_adc(5) , Cowref_adc5
     Readeeprom Wref_adc(6) , Cowref_adc6
     Wref_adc0(1) = Wref_adc(1)
     Wref_adc0(2) = Wref_adc(2)
     Wref_adc0(3) = Wref_adc(3)
     Wref_adc0(4) = Wref_adc(4)
     Wref_adc0(5) = Wref_adc(5)
     Wref_adc0(6) = Wref_adc(6)
     Idisplay = 0

End Sub

Sub Feepwrite(wdata As Word , Icoeep As Integer)
    Wref_adc(icoeep) = Wdata
    Select Case Icoeep
    Case 1 : Writeeeprom Wdata , Cowref_adc1
    Case 2 : Writeeeprom Wdata , Cowref_adc2
    Case 3 : Writeeeprom Wdata , Cowref_adc3
    Case 4 : Writeeeprom Wdata , Cowref_adc4
    Case 5 : Writeeeprom Wdata , Cowref_adc5
    Case 6 : Writeeeprom Wdata , Cowref_adc6
    Case Else:
    End Select
End Sub